#添加节点
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_node(1)
G.add_nodes_from([2, 3,4,5,6])
H = nx.path_graph(10)
print(H)
G.add_nodes_from(H)
# G.add_node(H)
G.add_edge(1, 2)
e = (2, 3)
G.add_edge(*e)  # unpack edge tuple*
G.clear()
G.add_edges_from([(1, 2), (1, 3)])
G.add_node(1)
G.add_edge(1, 2)
G.add_node("spam")        # adds node "spam"
G.add_nodes_from(["A","BB","CCC"])  # adds 4 nodes: 's', 'p', 'a', 'm'
# G.add_edge("A", 'BBB')
G.add_edges_from([("A", "BB"), ("BB", "CCC"),("BB","DDD")])

#统计边长和数量
print(G.number_of_nodes())
print(G.number_of_edges())

G = nx.Graph([(1, 2, {"color": "yellow"})])
# G.add_edge(1, 3)
# G[1][3]['color'] = "blue"
# G.edges[1, 2]['color'] = "red"

G.add_weighted_edges_from([(1, 2, 0.125), (1, 3, 0.75), (2, 4, 1.2), (3, 4, 0.375)])
for n, nbrs in G.adj.items():
   for nbr, eattr in nbrs.items():
       wt = eattr['weight']
       if wt < 0.5: print(f"({n}, {nbr}, {wt:.3})")
G = nx.Graph(day="Friday")
G.graph
G.add_node(1, time='5pm')
G.add_nodes_from([3], time='2pm')
G.nodes[1]['room'] = 714
G.nodes.data()

G.add_edge(1, 2, weight=4.7 )
G.add_edges_from([(3, 4), (4, 5)], color='red')
G.add_edges_from([(1, 2, {'color': 'yellow'}), (2, 3, {'weight': 8})])
G[1][2]['weight'] = 4.7
G.edges[3, 4]['weight'] = 4.2

G.add_nodes_from(["11123","net1233"], time='2pm',color='yellow')
print(nx.adjacency_spectrum(G))

print(G.edges(data=True))
print(G.nodes(data=True))

# nx.draw(G, with_labels=True,node_color=["tab:red" if n in I else "tab:blue" for n in G])
print("node_color",[n[1]["color"] if "color" in n[1] else "tab:blue" for n in G.nodes(data=True)])
print("edge_color",[n[2]["color"] if "color" in n[2] else "tab:blue" for n in G.edges(data=True)])
nx.draw(G, with_labels=True,
        node_color=[n[1]["color"] if "color" in n[1] else "tab:blue" for n in G.nodes(data=True)],
        edge_color = [n[2]["color"] if "color" in n[2] else "tab:blue" for n in G.edges(data=True)]
        )
plt.show()